<?php

namespace App\Http\Controllers\Api\v1\Astro;

use App\Http\Controllers\Api\v1\Astro\DataController;
use App\Http\Controllers\Api\BaseController;
use App\Http\Controllers\Api\v1\User\AstrosetController;
use App\Models\Api\Recode;

use Illuminate\Http\Request;

// 组合中点盘
class CompositeController extends BaseController
{

   /**
    * [get description]
    * @AuthorHTL kingcode
    * @DateTime  2018-12-14
    * @param     Request    $request [description]
    * @param     array      $data_id [description]
    * @return    [type]              [description]
    */
    public function get(Request $request, $data_id = [])
    {
        $id1 = $request->input('id1', 0);
        $id2 = $request->input('id2', 0);

        if (isset($data_id) && $data_id) {
            $id1 = $data_id['id1'];
            $id2 = $data_id['id2'];
        }

        $Data                  = new DataController();
        $Set                   = new AstrosetController();
        $astro_set             = $Set->get(true, 'composite'); // 内部调用
        $Data->planets_display = $astro_set['value']['planets_display'];
        $Data->asps_display    = $astro_set['value']['asps_display'];
        $Data->asp_orb_all     = $astro_set['value']['asp_orb_all'];
        $Data->planet_orb_all  = $astro_set['value']['planet_orb_all'];

        $map = ['id' => $id1, 'uid' => $request->uid];

        $RecodeModel = new Recode();

        $userinfo1 = $RecodeModel->getRecode($map);

        $timestamp1 = $Data->deal_time($userinfo1);
        $utdatenow1 = strftime("%d.%m.%Y", $timestamp1);
        $utnow1     = strftime("%H:%M:%S", $timestamp1);
        $longitude1 = ($userinfo1['long_deg'] + $userinfo1['long_min'] / 60) * $userinfo1['ew'];
        $latitude1  = ($userinfo1['lat_deg'] + $userinfo1['lat_min'] / 60) * $userinfo1['ns'];

        $map['id'] = $id2;

        $userinfo2  = $RecodeModel->getRecode($map);
        $timestamp2 = $Data->deal_time($userinfo2);
        $utdatenow2 = strftime("%d.%m.%Y", $timestamp2);
        $utnow2     = strftime("%H:%M:%S", $timestamp2);
        $longitude2 = ($userinfo2['long_deg'] + $userinfo2['long_min'] / 60) * $userinfo2['ew'];
        $latitude2  = ($userinfo2['lat_deg'] + $userinfo2['lat_min'] / 60) * $userinfo2['ns'];

        $options1 = ['utdatenow' => $utdatenow1, 'utnow' => $utnow1, 'longitude' => $longitude1, 'latitude' => $latitude1, 'h_sys' => $astro_set['value']['h_sys']];

        $out1 = $Data->execData($options1);

        $options2 = ['utdatenow' => $utdatenow2, 'utnow' => $utnow2, 'longitude' => $longitude2, 'latitude' => $latitude2, 'h_sys' => $astro_set['value']['h_sys']];

        $out2 = $Data->execData($options2);

        $original_data1 = $Data->getOriginalData($out1, $astro_set['value']['h_sys']);
        $original_data2 = $Data->getOriginalData($out2, $astro_set['value']['h_sys']);

        $original_data = $Data->getTheCompoisteCenter($original_data1, $original_data2);

        $Data->array_remove($original_data, $Data->planets_display);
        $details = $Data->getPlanetHouseDetail($original_data, $original_data['hc1']);
        $phase   = $Data->planetPkPlanet($original_data, $original_data, true);
        foreach ($phase['planets'] as $k => $d) {
            $details['planets'][$k]['phase'] = $d['phase'];
            if (empty($d['phase'])) {
                $details['planets'][$k]['phase'] = null;
            }

        }
        $details['phase'] = $phase['phase'];

        $details['planets'] = (object) $details['planets']; // 指定输出为object  防止所有行星全开时 类型改变

        if (isset($data_id) && $data_id) {
            return $details;
        } else {
            return show(config('code.success'), 'Success.', $details);
        }

    }
}
